import java.util.HashSet;
import java.util.Set;

/**
 * 128. 最长连续序列
 * https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked
 */
class Solution4 {
    public int longestConsecutive(int[] nums) {
        int n = nums.length;
        Set<Integer> set = new HashSet<>();

        for(int x : nums) {
            set.add(x);
        }

        int ret = 0;

        for(int x : set) {
            if(!set.contains(x - 1)) {
                int count = 1;
                int current = x;

                while(set.contains(current + 1)) {
                    current++;
                    count++;
                }

                ret = Math.max(ret, count);
            }
        }
        return ret;
    }
}